package main

import (
	"gitee.com/dennis-mxx/gcode-common/mcontext"
	"gitee.com/dennis-mxx/gcode-common/mcore"
	"gitee.com/dennis-mxx/gcode-data/mdata"
	"strconv"
	"xorm.io/builder"
)

type App struct {
	ID        int64  `xorm:"pk id autoincr bigint(20) notnull comment('id') " json:"id,omitempty"`
	AppId     string `xorm:"varchar(60) notnull index('app_id') comment('app编码') " json:"appId,omitempty"`
	AppName   string `xorm:"varchar(60) notnull comment('app名称') " json:"appName,omitempty"`
	Status    int    `xorm:"int notnull comment('状态  1 启用 2 禁用') " json:"status,omitempty"`
	mcore.Ext `xorm:"extends" `
}

func (ew App) TableName() string {
	return "tb_app"
}
func (ew App) IdName() string {
	return "id"
}
func (ew App) DB() string {
	return "default"
}

type AppFunc struct {
	m mdata.Mapper[App]
}

func NewAppFunc(mtx *mcontext.MContext) AppFunc {
	return AppFunc{m: mdata.NewMapper[App](mtx, App{})}
}

func (ew AppFunc) SelectActiveApp() []App {
	return ew.m.SelectListWatch(mdata.QueryParam{Cond: builder.Eq{"status": 1}})
}
func (ew AppFunc) SelectByAppId(appId string) *App {
	return ew.m.WatchGet(App{AppId: appId})
}

func (ew AppFunc) SelectPage() *mcore.Pageable {
	var build []builder.Cond
	return mcore.Watch(ew.m.SelectPage(mdata.QueryParam{Cond: builder.And(build...), Page: mcore.CmdPage{
		Page: mcore.String(strconv.Itoa(1)),
		Size: mcore.String(strconv.Itoa(10)),
	}}))
}
